home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / RCSKEEP.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  10KB  |  365 lines

  1. /*
  2.  *                     RCS keyword extraction
  3.  */
  4. /*****************************************************************************
  5.  *                       main routine: getoldkeys()
  6.  *                       Testprogram: define KEEPTEST
  7.  *****************************************************************************
  8.  */
  9.  
  10. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  11.    Copyright 1990 by Paul Eggert
  12.    Distributed under license by the Free Software Foundation, Inc.
  13.  
  14. This file is part of RCS.
  15.  
  16. RCS is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 1, or (at your option)
  19. any later version.
  20.  
  21. RCS is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with RCS; see the file COPYING.  If not, write to
  28. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  
  30. Report problems and direct all questions to:
  31.  
  32.     rcs-bugs@cs.purdue.edu
  33.  
  34. */
  35.  
  36.  
  37.  
  38. /* $Log: rcskeep.c%v $
  39.  * Revision 1.2  1991/08/23  13:35:13  SGP
  40.  * Ported to MSDOS using Borland C++
  41.  *
  42.  * Revision 5.2  1990/10/04  06:30:20  eggert
  43.  * Parse time zone offsets; future RCS versions may output them.
  44.  *
  45.  * Revision 5.1  1990/09/20  02:38:56  eggert
  46.  * ci -k now checks dates more thoroughly.
  47.  *
  48.  * Revision 5.0  1990/08/22  08:12:53  eggert
  49.  * Retrieve old log message if there is one.
  50.  * Don't require final newline.
  51.  * Remove compile-time limits; use malloc instead.  Tune.
  52.  * Permit dates past 1999/12/31.  Ansify and Posixate.
  53.  *
  54.  * Revision 4.6  89/05/01  15:12:56  narten
  55.  * changed copyright header to reflect current distribution rules
  56.  * 
  57.  * Revision 4.5  88/08/09  19:13:03  eggert
  58.  * Remove lint and speed up by making FILE *fp local, not global.
  59.  * 
  60.  * Revision 4.4  87/12/18  11:44:21  narten
  61.  * more lint cleanups (Guy Harris)
  62.  * 
  63.  * Revision 4.3  87/10/18  10:35:50  narten
  64.  * Updating version numbers. Changes relative to 1.1 actually relative
  65.  * to 4.1
  66.  * 
  67.  * Revision 1.3  87/09/24  14:00:00  narten
  68.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  69.  * warnings)
  70.  * 
  71.  * Revision 1.2  87/03/27  14:22:29  jenkins
  72.  * Port to suns
  73.  * 
  74.  * Revision 4.1  83/05/10  16:26:44  wft
  75.  * Added new markers Id and RCSfile; extraction added.
  76.  * Marker matching with trymatch().
  77.  * 
  78.  * Revision 3.2  82/12/24  12:08:26  wft
  79.  * added missing #endif.
  80.  *
  81.  * Revision 3.1  82/12/04  13:22:41  wft
  82.  * Initial revision.
  83.  *
  84.  */
  85.  
  86. /*
  87. #define KEEPTEST
  88. */
  89. /* Testprogram; prints out the keyword values found. */
  90.  
  91. #include  "rcsbase.h"
  92.  
  93. libId(keepId, "$Id: rcskeep.c%v 1.2 1991/08/23 13:35:13 SGP Exp $")
  94.  
  95. static int checknum P((const char*,int));
  96. static int getprevdate P((FILE*));
  97. static int getprevid P((int,FILE*,struct buf*));
  98. static int getprevrev P((FILE*));
  99. static int getval P((FILE*,struct buf*,int));
  100. static int get0val P((int,FILE*,struct buf*,int));
  101.  
  102. struct buf prevauthor, prevrev, prevstate;
  103. char prevdate[datesize];
  104.  
  105.     int
  106. getoldkeys(register FILE *fp)
  107. /* Function: Tries to read keyword values for author, date,
  108.  * revision number, and state out of the file fp.
  109.  * The results are placed into
  110.  * prevauthor, prevdate, prevrev, prevstate.
  111.  * Aborts immediately if it finds an error and returns false.
  112.  * If it returns true, it doesn't mean that any of the
  113.  * values were found; instead, check to see whether the corresponding arrays
  114.  * contain the empty string.
  115.  */
  116. {
  117.     register int c;
  118.     char keyword[keylength+1];
  119.     register char * tp;
  120.  
  121.     /* initialize to empty */
  122.     bufscpy(&prevauthor, "");
  123.     bufscpy(&prevrev, "");
  124.     bufscpy(&prevstate, "");
  125.     *prevdate = 0;
  126.  
  127.     while( (c=getc(fp)) != EOF) {
  128.         if ( c==KDELIM) {
  129.         do {
  130.         /* try to get keyword */
  131.         tp = keyword;
  132.         while ((c=getc(fp))!=EOF && c!='\n' && c!=KDELIM && c!=VDELIM 
  133.                && tp<keyword+keylength
  134.         )
  135.             *tp++ = c;
  136.         } while (c==KDELIM);
  137.         if (c==EOF)
  138.         break;
  139.             if (c!=VDELIM) continue;
  140.         *tp = c;
  141.         if ((c=getc(fp))!=' ' && c!='\t')
  142.         continue;
  143.  
  144.         switch (trymatch(keyword)) {
  145.             case Author:
  146.         if (!getprevid(0, fp, &prevauthor))
  147.             return false;
  148.         c = getc(fp);
  149.                 break;
  150.             case Date:
  151.         if (!(c = getprevdate(fp)))
  152.             return false;
  153.                 break;
  154.             case Header:
  155.             case Id:
  156.         if (!(
  157.               getval(fp, (struct buf*)nil, false) &&
  158.               getprevrev(fp) &&
  159.               (c = getprevdate(fp)) &&
  160.               getprevid(c, fp, &prevauthor) &&
  161.               getprevid(0, fp, &prevstate)
  162.         ))
  163.             return false;
  164.         /* Skip either ``who'' (new form) or ``Locker: who'' (old).  */
  165.         if (getval(fp, (struct buf*)nil, true) &&
  166.             getval(fp, (struct buf*)nil, true))
  167.             c = getc(fp);
  168.         else if (nerror)
  169.             return false;
  170.         else
  171.             c = KDELIM;
  172.         break;
  173.             case Locker:
  174.             case Log:
  175.             case RCSfile:
  176.             case Source:
  177.         if (!getval(fp, (struct buf*)nil, false))
  178.             return false;
  179.         c = getc(fp);
  180.                 break;
  181.             case Revision:
  182.         if (!getprevrev(fp))
  183.             return false;
  184.         c = getc(fp);
  185.                 break;
  186.             case State:
  187.         if (!getprevid(0, fp, &prevstate))
  188.             return false;
  189.         c = getc(fp);
  190.                 break;
  191.             default:
  192.                continue;
  193.             }
  194.         if (c != KDELIM) {
  195.         error("closing %c missing on keyword", KDELIM);
  196.         return false;
  197.         }
  198.         if (*prevauthor.string && *prevdate && *prevrev.string && *prevstate.string) {
  199.                 break;
  200.            }
  201.         }
  202.     }
  203.  
  204.     arewind(fp);
  205.     return true;
  206. }
  207.  
  208.  
  209.     static int
  210. getval(register FILE *fp, struct buf *target, int optional)
  211. /* Reads a keyword value from FP into TARGET.
  212.  * Returns true if one is found, false otherwise.
  213.  * Does not modify target if it is nil.
  214.  * Do not report an error if OPTIONAL is set and KDELIM is found instead.
  215.  */
  216. {
  217.     return get0val(getc(fp), fp, target, optional);
  218. }
  219.  
  220.     static int
  221. get0val(register int c, register FILE *fp, struct buf *target, int optional)
  222. /* Reads a keyword value from C+FP into TARGET, perhaps OPTIONALly.
  223.  * Same as getval, except C is the lookahead character.
  224.  */
  225. {   register char * tp;
  226.     const char *tlim;
  227.     register int got1;
  228.  
  229.     if (target) {
  230.     bufalloc(target, 1);
  231.     tp = target->string;
  232.     tlim = tp + target->size;
  233.     } else
  234.     tlim = tp = 0;
  235.     got1 = false;
  236.     for (;;  c = getc(fp))
  237.     switch (c) {
  238.         default:
  239.         got1 = true;
  240.         if (tp) {
  241.             *tp++ = c;
  242.             if (tlim <= tp)
  243.             tp = bufenlarge(target, &tlim);
  244.         }
  245.         continue;
  246.  
  247.         case ' ':
  248.         case '\t':
  249.         if (tp) {
  250.             *tp = 0;
  251. #            ifdef KEEPTEST
  252.             VOID printf("getval: %s\n", target);
  253. #            endif
  254.         }
  255.         if (!got1)
  256.             error("too much white space in keyword value");
  257.         return got1;
  258.  
  259.         case KDELIM:
  260.         if (!got1 && optional)
  261.             return false;
  262.         /* fall into */
  263.         case '\n':
  264.         case 0:
  265.         case EOF:
  266.         error("badly terminated keyword value");
  267.         return false;
  268.     }
  269. }
  270.  
  271.  
  272.     static int
  273. getprevdate(FILE *fp)
  274. /* Function: reads a date prevdate; checks format
  275.  * Return 0 on error, lookahead character otherwise.
  276.  */
  277. {
  278.     struct buf prevday, prevtime, prevzone, prev;
  279.     register const char *p;
  280.     register int c;
  281.  
  282.     c = 0;
  283.     bufautobegin(&prevday);
  284.     if (getval(fp,&prevday,false)) {
  285.     bufautobegin(&prevtime);
  286.     if (getval(fp,&prevtime,false)) {
  287.         bufautobegin(&prevzone);
  288.         bufscpy(&prevzone, "");
  289.         c = getc(fp);
  290.         if (c=='-' || c=='+')
  291.         c = get0val(c,fp,&prevzone,false) ? getc(fp) : 0;
  292.         if (c) {
  293.         bufautobegin(&prev);
  294.         p = prevday.string;
  295.         bufalloc(&prev, strlen(p) + strlen(prevtime.string) + strlen(prevzone.string) + 5);
  296.         VOID sprintf(prev.string, "%s%s %s %s", 
  297.             /* Parse dates put out by old versions of RCS.  */
  298.             isdigit(p[0]) && isdigit(p[1]) && p[2]=='/'  ?  "19"  :  "",
  299.             p, prevtime.string, prevzone.string
  300.         );
  301.         str2date(prev.string, prevdate);
  302.         bufautoend(&prev);
  303.         }
  304.         bufautoend(&prevzone);
  305.     }
  306.     bufautoend(&prevtime);
  307.     }
  308.     bufautoend(&prevday);
  309.     return c;
  310. }
  311.  
  312.     static int
  313. getprevid(int c, FILE *fp, struct buf *b)
  314. /* Get previous identifier from C+FP into B.  */
  315. {
  316.     if (!get0val(c?c:getc(fp), fp, b, false))
  317.         return false;
  318.     checksid(b->string);
  319.     return true;
  320. }
  321.  
  322.     static int
  323. getprevrev(FILE *fp)
  324. /* Get previous revision from FP into prevrev.  */
  325. {
  326.     return getval(fp,&prevrev,false) && checknum(prevrev.string,-1);
  327. }
  328.  
  329.  
  330.     static int
  331. checknum(register const char *sp,int fields)
  332. {    register int dotcount;
  333.      dotcount=0;
  334.      while(*sp) {
  335.         if (*sp=='.') dotcount++;
  336.     else if (!isdigit(*sp)) return false;
  337.         sp++;
  338.      }
  339.      return fields<0 ? dotcount&1 : dotcount==fields;
  340. }
  341.  
  342.  
  343.  
  344. #ifdef KEEPTEST
  345.  
  346. const char cmdid[] ="keeptest";
  347.  
  348.     int
  349. main(int argc, char *argv[])
  350. {
  351.         while (*(++argv)) {
  352.         FILE *f;
  353.         if (!(f = fopen(*argv,"r"))) {
  354.             perror(f);
  355.             exit(1);
  356.         }
  357.         getoldkeys(f);
  358.         VOID fclose(f);
  359.                 VOID printf("%s:  revision: %s, date: %s, author: %s, state: %s\n",
  360.                 *argv, prevrev.string, prevdate.string, prevauthor.string, prevstate.string);
  361.     }
  362.     exitmain(EXIT_SUCCESS);
  363. }
  364. #endif
  365.